package com.buka.ad.listener;


import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;

import com.buka.ad.config.RabbitMqConfig;
import com.buka.server.pojo.Ad;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class AdListener {
    private final String loadUrl = "http://192.168.232.102/ad_load";
    private final String readUrl = "http://192.168.232.102/ad_read";

    @RabbitListener(queues = RabbitMqConfig.AD_QUEUE)
    public void ad(Object message){
        try {
            //同步数据
            String s = HttpUtil.get(loadUrl);
            Map map = JSONObject.parseObject(s, Map.class);
            Boolean b = (boolean)map.get("flag");
            if (!b) {
                throw new RuntimeException();
            }
            //读取数据
            List<Ad> list = JSONObject.parseObject(HttpUtil.get(readUrl), List.class);
            System.out.println(list);
        } catch (RuntimeException e) {
            throw new RuntimeException(e);
        }
    }
}
